//// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
//package com.linqingying.gitcode.pullrequest.data.service
//
//import com.intellij.collaboration.async.CompletableFutureUtil.submitIOTask
//import com.intellij.openapi.diagnostic.logger
//import com.intellij.openapi.progress.ProgressIndicator
//import com.intellij.openapi.progress.ProgressManager
//import com.intellij.openapi.project.Project
//import com.linqingying.gitcode.api.*
//import com.linqingying.gitcode.api.data.GCRefUpdateRule
//import com.linqingying.gitcode.api.data.GCRepositoryPermissionLevel
//import com.linqingying.gitcode.api.data.GitCodeIssueState
//import com.linqingying.gitcode.api.data.GitCodePullRequestMergeMethod
//import com.linqingying.gitcode.pullrequest.GCPRStatisticsCollector
//import com.linqingying.gitcode.pullrequest.data.GCPRIdentifier
//import com.linqingying.gitcode.pullrequest.data.GCPRMergeabilityState
//import com.linqingying.gitcode.pullrequest.data.GCPRMergeabilityStateBuilder
//import com.linqingying.gitcode.pullrequest.data.service.GCServiceUtil.logError
//import java.util.concurrent.CompletableFuture
//
//class GCPRStateServiceImpl internal constructor(private val progressManager: ProgressManager,
//                                                private val project: Project,
//                                                private val securityService: GCPRSecurityService,
//                                                private val requestExecutor: GitCodeApiRequestExecutor,
//                                                private val serverPath: GitCodeServerPath,
//                                                private val repoPath: GCRepositoryPath)
//  : GCPRStateService {
//
//  private val repository = GCRepositoryCoordinates(serverPath, repoPath)
//
//  override fun loadMergeabilityState(progressIndicator: ProgressIndicator,
//                                     pullRequestId: GCPRIdentifier,
//                                     headRefOid: String,
//                                     prHtmlUrl: String,
//                                     baseRefUpdateRule: GCRefUpdateRule?): CompletableFuture<GCPRMergeabilityState> =
//    progressManager.submitIOTask(progressIndicator) {
//      val mergeabilityData = requestExecutor.execute(it, GCGQLRequests.PullRequest.mergeabilityData(repository, pullRequestId.number))
//                             ?: error("Could not find pull request ${pullRequestId.number}")
//      val builder = GCPRMergeabilityStateBuilder(headRefOid, prHtmlUrl, mergeabilityData)
//      if (baseRefUpdateRule != null) {
//        builder.withRestrictions(securityService.currentUserHasPermissionLevel(GCRepositoryPermissionLevel.ADMIN), baseRefUpdateRule)
//      }
//      builder.build()
//    }.logError(LOG, "Error occurred while loading mergeability state data for PR ${pullRequestId.number}")
//
//
//  override fun close(progressIndicator: ProgressIndicator, pullRequestId: GCPRIdentifier) =
//    progressManager.submitIOTask(progressIndicator) {
//      requestExecutor.execute(it,
//                              GitCodeApiRequests.Repos.PullRequests.update(serverPath, repoPath.owner, repoPath.repository,
//                                                                          pullRequestId.number,
//                                                                          state = GitCodeIssueState.closed))
//      return@submitIOTask
//    }.logError(LOG, "Error occurred while closing PR ${pullRequestId.number}")
//
//  override fun reopen(progressIndicator: ProgressIndicator, pullRequestId: GCPRIdentifier) =
//    progressManager.submitIOTask(progressIndicator) {
//      requestExecutor.execute(it,
//                              GitCodeApiRequests.Repos.PullRequests.update(serverPath, repoPath.owner, repoPath.repository,
//                                                                          pullRequestId.number,
//                                                                          state = GitCodeIssueState.open))
//      return@submitIOTask
//    }.logError(LOG, "Error occurred while reopening PR ${pullRequestId.number}")
//
//  override fun markReadyForReview(progressIndicator: ProgressIndicator, pullRequestId: GCPRIdentifier) =
//    progressManager.submitIOTask(progressIndicator) {
//      requestExecutor.execute(it,
//                              GCGQLRequests.PullRequest.markReadyForReview(repository, pullRequestId.id))
//      return@submitIOTask
//    }.logError(LOG, "Error occurred while marking PR ${pullRequestId.number} ready fro review")
//
//  override fun merge(progressIndicator: ProgressIndicator, pullRequestId: GCPRIdentifier,
//                     commitMessage: Pair<String, String>, currentHeadRef: String) =
//    progressManager.submitIOTask(progressIndicator) {
//      requestExecutor.execute(it, GitCodeApiRequests.Repos.PullRequests.merge(serverPath, repoPath, pullRequestId.number,
//                                                                             commitMessage.first, commitMessage.second,
//                                                                             currentHeadRef))
//      GCPRStatisticsCollector.logMergedEvent(project, GitCodePullRequestMergeMethod.merge)
//      return@submitIOTask
//    }.logError(LOG, "Error occurred while merging PR ${pullRequestId.number}")
//
//
//  override fun rebaseMerge(progressIndicator: ProgressIndicator, pullRequestId: GCPRIdentifier,
//                           currentHeadRef: String) =
//    progressManager.submitIOTask(progressIndicator) {
//      requestExecutor.execute(it,
//                              GitCodeApiRequests.Repos.PullRequests.rebaseMerge(serverPath, repoPath, pullRequestId.number,
//                                                                               currentHeadRef))
//      GCPRStatisticsCollector.logMergedEvent(project, GitCodePullRequestMergeMethod.rebase)
//      return@submitIOTask
//    }.logError(LOG, "Error occurred while rebasing PR ${pullRequestId.number}")
//
//  override fun squashMerge(progressIndicator: ProgressIndicator, pullRequestId: GCPRIdentifier,
//                           commitMessage: Pair<String, String>, currentHeadRef: String) =
//    progressManager.submitIOTask(progressIndicator) {
//      requestExecutor.execute(it,
//                              GitCodeApiRequests.Repos.PullRequests.squashMerge(serverPath, repoPath, pullRequestId.number,
//                                                                               commitMessage.first, commitMessage.second,
//                                                                               currentHeadRef))
//      GCPRStatisticsCollector.logMergedEvent(project, GitCodePullRequestMergeMethod.squash)
//      return@submitIOTask
//    }.logError(LOG, "Error occurred while squash-merging PR ${pullRequestId.number}")
//
//  companion object {
//    private val LOG = logger<GCPRStateService>()
//  }
//}
